﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietPremier.Areas.AdminCP.Models;
using MvcLibrary.Repository;
using System.IO;

namespace VietPremier.Areas.AdminCP.Controllers
{
    public class AdminStyleController : Controller
    {
        #region "Index"
        public ActionResult Index()
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                TravelStyleModel model = new TravelStyleModel();

                foreach (var travelStyle in db.EntityStyles)
                {
                    model.listTravelStyle.Add(new TravelStyle()
                    {
                        Id = travelStyle.Id,
                        Name = travelStyle.Name,
                        Position = travelStyle.Position,
                        //<!-- SEO -->
                        Titletag = travelStyle.Titletag,
                        Meta_description = travelStyle.Meta_description,
                        Meta_keyword = travelStyle.Meta_keyword
                    });
                }
                return View(model);
            }
        }
        #endregion //index

        #region "Delete"
        public string Delete(int id)
        {
            try
            {
                dbVietPremierDataContext db = new dbVietPremierDataContext();
                EntityStyle item = db.EntityStyles.Where(p => p.Id == id).Single();

                db.EntityStyles.DeleteOnSubmit(item);
                db.SubmitChanges();
                return "Ok";
            }
            catch
            {
                return "Error";

            }

        }
        #endregion

        #region "Insert"
        public ActionResult Insert()
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                TravelStyle model = new TravelStyle();
                return PartialView(model);
            }
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Insert(TravelStyle model)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                EntityStyle travelStyle = new EntityStyle();
                travelStyle.Name = model.Name;
                travelStyle.Position = model.Position;
                
                //<!-- SEO -->
                travelStyle.Titletag = model.Titletag;
                travelStyle.Meta_description = model.Meta_description;
                travelStyle.Meta_keyword = model.Meta_keyword;

                db.EntityStyles.InsertOnSubmit(travelStyle);
                db.SubmitChanges();

                return RedirectToAction("Index", "AdminStyle");
            }
        }
        #endregion

        #region "Edit"
        public ActionResult Edit(int idTravelStyle)
        {
            TravelStyle model = new TravelStyle();
            return View(model);
        }

        [HttpPost]
        public JsonResult GetDetail(int idTravelStyle)
        {
            TravelStyle model = new TravelStyle();
            model.Id = -1;

            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                EntityStyle travelStyle = db.EntityStyles.FirstOrDefault(o => o.Id == idTravelStyle);

                if (travelStyle != null)
                {
                    model.Id = travelStyle.Id;
                    model.Name = travelStyle.Name;
                    model.Position = travelStyle.Position;
                    //<!--SEO -->
                    model.Titletag = travelStyle.Titletag;
                    model.Meta_description = travelStyle.Meta_description;
                    model.Meta_keyword = travelStyle.Meta_keyword;
                }
                return Json(model);
            }

        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(int idTravelStyle, TravelStyle model)
        {

            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                //check Id 
                EntityStyle travelStyle = db.EntityStyles.Where(p => p.Id == idTravelStyle).FirstOrDefault();
                travelStyle.Position = model.Position;
                travelStyle.Name = model.Name;
               
                //<!--SEO -->
                travelStyle.Titletag = model.Titletag;
                travelStyle.Meta_description = model.Meta_description;
                travelStyle.Meta_keyword = model.Meta_keyword;

                db.SubmitChanges();
                return RedirectToAction("Index", "AdminStyle");
            }
        }
        #endregion //edit
    }
}
